[Scala] Applying overloaded, typed methods on a collection
Posted
by stephanos
on Stack Overflow
See other posts from Stack Overflow
or by stephanos
Published on 2010-06-03T10:38:13Z
Indexed on
2010/06/03
16:14 UTC
Read the original article
Hit count: 140
scala
I'm quite new to Scala and struggling with the following:
I have database objects (type of BaseDoc) and value objects (type of BaseVO). Now there are multiple convert methods (all called 'convert') that take an instance of an object and convert it to the other type accordingly - like this:
def convert(doc: ClickDoc): ClickVO = ...
def convert(doc: PointDoc): PointVO = ...
def convert(doc: WindowDoc): WindowVO = ...
Now I sometimes need to convert a list of objects. How would I do this - I tried:
def convert[D <: BaseDoc, V <: BaseVO](docs: List[D]):List[V] = docs match {
case List() => List()
case xs => xs.map(doc => convert(doc))
}
Which results in 'overloaded method value convert with alternatives ...'. I tried to add manifest information to it, but couldn't make it work.
I couldn't even create one method for each because it'd say that they have the same parameter type after type erasure (List).
Ideas welcome!
© Stack Overflow or respective owner